package com.tianfeng.fenghuotoutiao.presenter.ads.customer;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.tianfeng.fenghuotoutiao.R;
import com.tianfeng.fenghuotoutiao.globle.ConstantUrl;
import com.tianfeng.fenghuotoutiao.ui.activity.UserNowTixianActivity;
import com.tianfeng.fenghuotoutiao.ui.activity.WebHelperActivity;
import com.tianfeng.fenghuotoutiao.ui.activity.video.UserNowTixianStatus1Activity;
import com.tianfeng.fenghuotoutiao.ui.adapter.FloatAdBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Dinosa on 2018/3/28.
 * 这里是自定义浮标的业务逻辑了；
 */

public class CustomFloatAd {


    public void init(View helper, final FloatAdBean adBean, final Context context,final String uid){

        final ImageView view = (ImageView) helper.findViewById(R.id.iv_float_icon);
       // ImageUtils.loadImage(context,adBean.getImgurl(),view);

        Glide.with(context)
                .load(R.drawable.bg_guide_login)
                .dontAnimate()
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        view.setImageDrawable(resource);
                    }
                });


        helper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gomethod(uid,context);
//
//                if (!TextUtils.isEmpty(adBean.getDownurl())) {
////                    //这里要做的逻辑就是判断开启任务下载
////                    Intent intent = new Intent(context, AdDownloadServiceNew.class);
////                    intent.putExtra(AdDownloadServiceNew.DOWNLOAD_INTERFACE_BEAN,adBean);
////                    context.startService(intent);
//
//                    Intent webIntent = WebHelperActivity.getIntent(
//                            context,
//                            ConstantUrl.JKD_WEB_URL + uid+"&time="+System.currentTimeMillis(),
//                            context.getString(R.string.main_tab_name_apprentice),
//                            false);
//                    context.startActivity(webIntent);
//                }else {
//                    Intent webIntent = WebHelperActivity.getIntent(
//                            context,
//                            ConstantUrl.JKD_WEB_URL + uid+"&time="+System.currentTimeMillis(),
//                            context.getString(R.string.main_tab_name_apprentice),
//                            false);
//                    context.startActivity(webIntent);
//                    //这里表示要进入一个网页的操作；
//                    context.startActivity(WebHelperActivity.getIntent(context,adBean.getUrl(),adBean.getTitle(),false));
//                }
            }
        });
    }
    /**
     * 提现东西提交
     */
    public void gomethod(String id,Context context){
        OkHttpClient okHttpClient=new OkHttpClient();
        final Request request = new Request.Builder()
                .url("http://tianze1688.com/api/index.php/news/user/selectrMethod?osversion=7.1.2&sign=4524d2ddd1e6365daf499c914c425423&versionCode=18&adCode=18&timesc=1557912690304&phonemodel=xiaomi&phonetype=2&deviceid=864699035010366&channel=8&from_uid="+id+"&status="+"0")
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                if (TextUtils.isEmpty(result)) return;
                JSONObject jsonObject= null;
                try {
                    jsonObject = new JSONObject(result);
                    String data=jsonObject.optString("data");

                    if (TextUtils.isEmpty(data)){

                        context.startActivity(new Intent(context, UserNowTixianActivity.class));
                    }else {
                        context.startActivity(new Intent(context, UserNowTixianStatus1Activity.class));

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        });
    }
}
